<?php

namespace app\admin\controller;

use think\facade\Db;

class Role extends Base
{

    protected function _infoModule()
    {
        return array(
            'info' => array(
                'name' => '角色管理',
                'description' => '游戏角色',
            ),
            'menu' => array(
                array(
                    'name' => '列表',
                    'url' => url('index'),
                    'icon' => 'list',
                ),
            ),
            '_info' => array(
                array(
                    'name' => '添加',
                    'url' => url('info'),
                ),
            ),
        );
    }

    public function index()
    {
        //筛选条件
        $where = array();

        $id = input('id');
        $uname = input('uname');

        if ($id) {
            $where['uid'] = $id;
        }
        if ($uname) {
            $where['uname'] = ['like','%'.$uname.'%'];
        }

        //URL参数
        $pageMaps = array();
        $pageMaps['id'] = $id;
        $pageMaps['uname'] = $uname;

        $pageAry = [
            'list_rows' => 15,
            'page' => input('page', 1),
            'path' => 'index',
            'query' => $pageMaps,
        ];

        //查询数据
        $list = model('Role')->loadList($where, $pageAry);
        if(!empty($list)){
            foreach ($list as $key=>$val){
                $val['tool1Name'] = $this->getZhuangbei($val['tool1']);
                $val['tool2Name'] = $this->getZhuangbei($val['tool2']);
                $val['tool3Name'] = $this->getZhuangbei($val['tool3']);
                $val['tool4Name'] = $this->getZhuangbei($val['tool4']);
                $val['tool5Name'] = $this->getZhuangbei($val['tool5']);
                $val['tool6Name'] = $this->getZhuangbei($val['tool6']);
            }
        }
        $this->assign('list', $list);
        $this->assign('_page', $list->render());
        $this->assign('pageMaps', $pageMaps);

        $this->fetch('index');
    }
    
    public function getZhuangbei($zbid)
    {
        if($zbid != 0){
            $zbname = model('RoleEquip')->getWhereValue(['zbnowid'=>$zbid],'zbname');
            return $zbname;
        }else{
            return '';
        }
    }

    public function info()
    {
        $request = request();
        $id = input('uid');
        $userModel = model('Role');
        if ($request->method() == 'POST') {
            $status = false;
            if ($id) {
                $status = $userModel->editAll($request);
            }
            if ($status !== false) {
                returnJson(1, '操作成功', ['url'=>url('index')]);
            } else {
                returnJson(0, '操作失败');
            }
        } else {
            $info = $userModel->getRoleAttribute($id);
            if(!empty($info)){
                $info['infoPets'] = [];
                if($info['cw']!=0){
                    $info['infoPets'] = model('RolePets')->getInfo($info['cw'])->toArray();
                }

                $info['infoMapName'] = [];
                if($info['nowmid']!=0){
                    $info['infoMapName'] = model('MapArea')->findValue(['mid'=>$info['nowmid']],'mname');
                }

                $info['tool1Name'] = $this->getZhuangbei($info['tool1']);
                $info['tool2Name'] = $this->getZhuangbei($info['tool2']);
                $info['tool3Name'] = $this->getZhuangbei($info['tool3']);
                $info['tool4Name'] = $this->getZhuangbei($info['tool4']);
                $info['tool5Name'] = $this->getZhuangbei($info['tool5']);
                $info['tool6Name'] = $this->getZhuangbei($info['tool6']);
            }
            $this->assign('info', $info);

            //
            $mapEquip[] = ['uid', '=', $info['uid']];
            $mapEquip[] = ['zbnowid', 'not in', [$info['tool1'],$info['tool2'],$info['tool3'],$info['tool4'],$info['tool5'],$info['tool6']]];
            $listRoleEquip = model('RoleEquip')->getList($mapEquip);
            if(!empty($listRoleEquip)){
                foreach ($listRoleEquip as $item){
                    $item['zbtoolName'] = model('Equip')->getZbtoolAttr($item['zbtool']);
                }
            }
            $this->assign('listRoleEquip', $listRoleEquip);

            //
            $mapProp[] = ['uid', '=', $info['uid']];
            $listRoleProp = model('RoleProp')->getList($mapProp);
            if(!empty($listRoleProp)){
                foreach ($listRoleProp as $item){
                    $item['djzlName'] = model('Prop')->getDjzlAttr($item['djzl']);
                }
            }
            $this->assign('listRoleProp', $listRoleProp);

            //
            $mapDrug[] = ['uid', '=', $info['uid']];
            $listRoleDrug = model('RoleDrug')->getList($mapDrug);
            if(!empty($listRoleDrug)){
                foreach ($listRoleDrug as $item){
                    $item['typeName'] = model('Drug')->getTypeAttr($item['type']);
                }
            }
            $this->assign('listRoleDrug', $listRoleDrug);

            //
            $mapSkill[] = ['uid', '=', $info['uid']];
            $listRoleSkill = model('RoleSkill')->getList($mapSkill);
            $this->assign('listRoleSkill', $listRoleSkill);

            //
            $mapPets[] = ['uid', '=', $info['uid']];
            $listRolePets = model('RolePets')->getList($mapPets);
            if(!empty($listRolePets)){
                foreach ($listRolePets as $item){
                    $item['grow_up'] = model('Pets')->findValue(['id'=>$item['pid']],'grow_up');
                }
            }
            $this->assign('listRolePets', $listRolePets);

            //
            $mapTask[] = ['uid', '=', $info['uid']];
            $listRoleTask = model('RoleTask')->getList($mapTask);
            if(!empty($listRoleTask)){
                foreach ($listRoleTask as $val){
                    $val['rwlxName'] = ($val['rwlx']==1) ? '普通' : (($val['rwlx']==2) ? '每日' : '主线');
                    $val['rwzlName'] = ($val['rwzl']==1) ? '收集' : (($val['rwzl']==2) ? '打怪' : '对话');

                    if($val['rwzl']==1){
                        $rwyq_name = model('Prop')->findValue(['djid'=>$val['rwyq']],'djname');
                        $val['rwyq_name'] = $rwyq_name . ' x ' . $val['rwcount'];
                    }
                    if($val['rwzl']==2){
                        $rwyq_name = model('Monster')->findValue(['id'=>$val['rwyq']],'gname');
                        $val['rwyq_name'] = $rwyq_name . ' x ' . $val['rwcount'];
                    }
                    if($val['rwzl']==3){
                        $rwyq_name = model('Npc')->findValue(['id'=>$val['rwyq']],'nname');
                        $rwcount_name = model('Npc')->findValue(['id'=>$val['rwcount']],'nname');
                        $val['rwyq_name'] = $rwyq_name . ' -> ' . $rwcount_name;
                    }

                    $val['rwzb_name'] = $this->strSelect($val['rwzb'],1);
                    $val['rwdj_name'] = $this->strSelect($val['rwdj'],2);
                    $val['rwyp_name'] = $this->strSelect($val['rwyp'],3);
                    $val['rwjn_name'] = model('Skill')->findValue(['jnid'=>$val['rwjineng']],'jnname');
                }
            }
            $this->assign('listRoleTask', $listRoleTask);

            $this->fetch('info');
        }
    }

    public function strSelect($strIds,$type){
        $ary = ($strIds != '' && $strIds != null) ? explode(',', $strIds) : [];
        $list = [];
        if (!empty($ary)) {
            foreach ($ary as $k => $item) {
                $aryInfo = explode('|', $item);

                $name = '';
                if($type==1){
                    $name = model('Equip')->findValue(['zbid'=>$aryInfo[0]],'zbname');
                    $list[$k]['lv'] = model('Equip')->findValue(['zbid'=>$aryInfo[0]],'zblv');
                }
                if($type==2){
                    $name = model('Prop')->findValue(['djid'=>$aryInfo[0]],'djname');
                }
                if($type==3){
                    $name = model('Drug')->findValue(['ypid'=>$aryInfo[0]],'ypname');
                    $list[$k]['lv'] = model('Drug')->findValue(['ypid'=>$aryInfo[0]],'yplv');
                }

                $list[$k]['id'] = $aryInfo[0];
                $list[$k]['num'] = empty($aryInfo[1]) ? 1 : $aryInfo[1];
                $list[$k]['name'] = $name;
            }
        }
        return $list;
    }

    public function getRoleEquip()
    {
        $id = input('id');
        $info = model('RoleEquip')->getInfo($id);
        if(!empty($info)){
            returnJson(1,'ok',$info);
        }else{
            returnJson(0,'数据不存在');
        }
    }

    public function editProp()
    {
        $request = request();
        $id = input('id');
        $model = model('RoleProp');
        if ($request->method() == 'POST') {
            $status = false;
            if ($id) {
                $temp = ['djsum' => input('sum')];
                $model->where('id',$id)->update($temp);
                $status = true;
            }
            if ($status !== false) {
                returnJson(1, '操作成功', ['url'=>url('index')]);
            } else {
                returnJson(0, '操作失败');
            }
        }else{
            $info = $model->getInfo($id);
            if(!empty($info)){
                $info['sum'] = $info['djsum'];
            }
            $this->assign('info', $info);

            $this->fetch('edit');
        }
    }

    public function editDrug()
    {
        $request = request();
        $id = input('id');
        $model = model('RoleDrug');
        if ($request->method() == 'POST') {
            $status = false;
            if ($id) {
                $temp = ['ypsum' => input('sum')];
                $model->where('id',$id)->update($temp);
                $status = true;
            }
            if ($status !== false) {
                returnJson(1, '操作成功', ['url'=>url('index')]);
            } else {
                returnJson(0, '操作失败');
            }
        }else{
            $info = $model->getInfo($id);
            if(!empty($info)){
                $info['sum'] = $info['ypsum'];
            }
            $this->assign('info', $info);

            $this->fetch('edit');
        }
    }

    public function editSkill()
    {
        $request = request();
        $id = input('id');
        $model = model('RoleSkill');
        if ($request->method() == 'POST') {
            $status = false;
            if ($id) {
                $temp = ['jncount' => input('sum')];
                $model->where('id',$id)->update($temp);
                $status = true;
            }
            if ($status !== false) {
                returnJson(1, '操作成功', ['url'=>url('index')]);
            } else {
                returnJson(0, '操作失败');
            }
        }else{
            $info = $model->getInfo($id);
            if(!empty($info)){
                $info['sum'] = $info['jncount'];
            }
            $this->assign('info', $info);

            $this->fetch('edit');
        }
    }

    /**
     * 删除信息
     * @param int $id
     * @return bool 删除状态
     */
    public function del()
    {
        $id = input('id');
        if (empty($id)) {
            returnJson(0, '参数不能为空');
        }

        $userModel = model('Role');
        if ($userModel->del($id)) {
            returnJson(1, '删除成功！');
        } else {
            returnJson(0, '删除失败');
        }
    }
    public function delEquip()
    {
        $id = input('id');
        if (empty($id)) {
            returnJson(0, '参数不能为空');
        }

        $userModel = model('RoleEquip');
        if ($userModel->del($id)) {
            returnJson(1, '删除成功！');
        } else {
            returnJson(0, '删除失败');
        }
    }
    public function delProp()
    {
        $id = input('id');
        if (empty($id)) {
            returnJson(0, '参数不能为空');
        }

        $userModel = model('RoleProp');
        if ($userModel->del($id)) {
            returnJson(1, '删除成功！');
        } else {
            returnJson(0, '删除失败');
        }
    }
    public function delDrug()
    {
        $id = input('id');
        if (empty($id)) {
            returnJson(0, '参数不能为空');
        }

        $userModel = model('RoleDrug');
        if ($userModel->del($id)) {
            returnJson(1, '删除成功！');
        } else {
            returnJson(0, '删除失败');
        }
    }
    public function delSkill()
    {
        $id = input('id');
        if (empty($id)) {
            returnJson(0, '参数不能为空');
        }

        $userModel = model('RoleSkill');
        if ($userModel->del($id)) {
            returnJson(1, '删除成功！');
        } else {
            returnJson(0, '删除失败');
        }
    }
    public function delPets()
    {
        $id = input('id');
        if (empty($id)) {
            returnJson(0, '参数不能为空');
        }

        $userModel = model('RolePets');
        if ($userModel->del($id)) {
            returnJson(1, '删除成功！');
        } else {
            returnJson(0, '删除失败');
        }
    }
    public function delTask()
    {
        $id = input('id');
        if (empty($id)) {
            returnJson(0, '参数不能为空');
        }

        $userModel = model('RoleTask');
        if ($userModel->del($id)) {
            returnJson(1, '删除成功！');
        } else {
            returnJson(0, '删除失败');
        }
    }

}
